<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
//$this->model('task')->test();
$return['status']=$this->member_status;
$tabs=array(
	array('op'=>'list','title'=>'全部用户'),
	array('op'=>'edit','title'=>'编辑用户'),
); 
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['jsjiqi'])){
		$params['jsjiqi']=intval($_RQ['jsjiqi']);
		$return['params']['jsjiqi']=intval($_RQ['jsjiqi']);
	}
	if($_RQ['mid']){
		$params['mid']=$_RQ['mid'];
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['level_id']){
		$params['level_id']=$_RQ['level_id'];
		$return['params']['level_id']=$_RQ['level_id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:10;
	$list=pdo_getslice('jy_newganfee_member',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$credit=pdo_get('core_members',array('id'=>$item['mid']),array('credit2','credit3','credit3_fz'));
			$item['credit']=$credit;
			$item['credit2']=$credit['credit2'];
			$item['credit3']=$credit['credit3'];
			$item['credit3_fz']=$credit['credit3_fz'];
			if($item['fromuser']){
				$item['agentmember']=cfc('core')->member_show($item['fromuser']);
			}else{
				$item['agentmember']=array('nickname'=>'无');
			}
			$downmids=pdo_getall('jy_newganfee_member',array('fromuser'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
			if($downmids){
					$downmids=array_keys($downmids);
					$canyus=pdo_getnorepeat('jy_weishop_order',array('ispay'=>1,'mid'=>$downmids,'uniacid'=>$_SESSION['uniacid']),'mid');
					$item['downnum']=count($canyus);
			}
			$item['downnum2']=$this->model('model')->get_downnum($item['mid']);
			$item['yongfee']=pdo_getsum('jy_newganfee_commission',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'money');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：</br>1、设置--基本设置--任务解锁---否开启任务解锁功能，分销才会生效！！',
		'total'=>$total,
		'psize'=>$psize,
		'list'=>array(
			array('name'=>'mid','title'=>'会员id','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'agentmember','title'=>'上级信息','type'=>'member'),
			array('name'=>'level_id','title'=>'分销等级','type'=>'select','options'=>'levels'),
			array('name'=>'downnum2','title'=>'团队人数','type'=>'string'),
			array('name'=>'downnum','title'=>'推荐参与人次','type'=>'string'),
			//array('name'=>'jiqiendtime','title'=>'机器人到期时间','type'=>'time'),
			//array('name'=>'jsjiqi','title'=>'已开通机器人','type'=>'radio','options'=>'jsjiqi'),
			array('type'=>'img_string','title'=>'余额/佣金',
			'data'=>array(
				array('title'=>'余额：','name'=>'credit2'),
				array('title'=>'佣金：','name'=>'yongfee')
				)
			),
			array('name'=>'baofee','title'=>'保证金','type'=>'string'),
			array('name'=>'freeze','title'=>'冻结额','type'=>'string'),
			array('type'=>'img_string','title'=>'虚拟币',
			'data'=>array(
				array('title'=>'虚拟币：','name'=>'credit3'),
				array('title'=>'冻结虚拟币：','name'=>'credit3_fz')
				)
			),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'chongzhi','title'=>'充值保证金'),
			array('type'=>'modal','op'=>'jiedong','title'=>'手动解冻'),
			array('type'=>'href','op'=>'member','title'=>'他的直推'),
			array('type'=>'href','op'=>'change','title'=>'修改推荐人'),
			array('type'=>'modal','op'=>'setlevel','title'=>'设置等级'),
		)),
		'search'=>array(
			array('name'=>'jsjiqi','width'=>15,'title'=>'是否开通机器人','type'=>'radio','options'=>'jsjiqi'),
			array('name'=>'level_id','width'=>15,'title'=>'分销等级','type'=>'select','options'=>'levels'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),

		)
	);
	$return['jsjiqi']=array('未开通','已开通');
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_newganfee_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('get_type'=>0);
	}
	$item['jiqiendtime']=date('Y-m-d H:i:s',$item['jiqiendtime']);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回用户列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('name'=>'jiqiendtime','title'=>'机器人到期时间','type'=>'time'),
								array('name'=>'jsjiqi','title'=>'已开通机器人','type'=>'radio','options'=>'jsjiqi'),
								array('name'=>'freeze','title'=>'冻结额','type'=>'string'),
								array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
							)
						),
			
			)
	);
	$return['jsjiqi']=array('未开通','已开通');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'jsjiqi'=>intval($postdata['jsjiqi']),
		'jiqiendtime'=>strtotime($postdata['jiqiendtime']),
		'status'=>intval($postdata['status']),
		'freeze'=>$postdata['freeze'],
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_newganfee_member',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_newganfee_member',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$fromuser=intval($post['fromuser']);
		if($fromuser && $fromuser==$mid){
			exi('上级不能是自己！','error');
		}
		pdo_update('jy_newganfee_member',array('fromuser'=>$fromuser),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$this->model('member')->update_agent_level($fromuser);
		exi('修改推荐人成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])),array('mid','fromuser'));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改推荐人',
		'edit'=>array(
			array('type'=>'choose','name'=>'fromuser','title'=>'设置推荐人','action'=>'core/member.get_member/','help'=>'填写0表示设置为总店'),
		),
	);
	exi($return);
}
if($_RQ['op']=='setlevel'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->update('member',array('level_id'=>intval($post['level_id'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定分销不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置等级',
		'edit'=>array(
			array('name'=>'level_id','title'=>'等级','type'=>'select','options'=>'levels'),
		),
	);
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='chongzhi'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(intval($post['money'])<=0){
			exi('请先设置充值金额！并且为整数！','error');
		}
		$fee=sprintf("%.2f", intval($post['money']));
		/*$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$post['mid'],
			'fee'=>$fee,
			'status'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('core_recharge',$data);*/
		$this->model('member')->recharge($post['mid'],$fee,0,'充值保证金');
		exi('充值保证金成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'手动充值保证金',
		'edit'=>array(
			array('name'=>'tip','title'=>'<red>请谨慎操作</red>，后台手动充值保证金，消息记录会产生充值记录!','type'=>'tip'),
			array('name'=>'money','title'=>'要充值的保证金金额','type'=>'string'),
		),
	);
	exi($return);
}
if($_RQ['op']=='jiedong'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if($post['fee']>$post['freeze']){
			exi('要解冻的金额不能大于当前冻结！','error');
		}
		$this->model('member')->recharge($post['mid'],$post['fee'],3,'手动解冻');
		exi('解冻成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定分销不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'手动解冻',
		'edit'=>array(
			array('name'=>'tip','title'=>'<red>请谨慎操作</red>，只有出现异常,没办法时才可操作!','type'=>'tip'),
			array('name'=>'freeze','title'=>'当前冻结','type'=>'copy'),
			array('name'=>'fee','title'=>'要解冻的金额','type'=>'string'),
		),
	);
	exi($return);
}
if($_RQ['op']=='member'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	if($_RQ['mid']){
		$params=array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>intval($_RQ['mid']));
	}
	if($_RQ['id']){
		$mid=$this->pdo_getcolumn('member',array('id'=>$_RQ['id']),'mid');
		$params=array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$mid);
	}
	$return['params']['mid']=$params['fromuser'];
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'createtime desc',
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['level_id']=$item['level_id'];
			$item['teamstatus']=$item['teamstatus'];
			$item['order_num']=$item['order_num'];
			$item['order_price']=$item['order_price'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export2',
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'level_id','title'=>'团队长等级','type'=>'select','options'=>'levels'),
		),'action'=>array(
			array('type'=>'href','op'=>'member','title'=>'他的下线'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择上级'),
			array('name'=>'createtime','width'=>15,'title'=>'加入时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_newganfee_member',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_newganfee_member',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}